package com.bawei.main


import android.Manifest
import android.content.Intent
import android.net.Uri
import android.os.Build
import android.provider.Settings
import androidx.navigation.NavController
import androidx.navigation.fragment.NavHostFragment
import androidx.navigation.ui.setupWithNavController
import com.alibaba.android.arouter.facade.annotation.Route
import com.bawei.base.RoutePath
import com.bawei.common.base.BaseActivity
import com.bawei.main.databinding.ActivityMainBinding
<<<<<<< HEAD
//import com.tbruyelle.rxpermissions2.RxPermissions

//import com.tbruyelle.rxpermissions2.RxPermissions


//import com.tbruyelle.rxpermissions2.RxPermissions

=======

>>>>>>> 4bd9ee2c4d9f0e81098ef6d1abae9f2eb7ec6c07

@Route(path = RoutePath.PATH_MAIN_PAGE)
class MainActivity : BaseActivity<ActivityMainBinding>() {

    private lateinit var navHostFragment: NavHostFragment
    private lateinit var navController: NavController
    override fun getLayoutId(): Int = R.layout.activity_main

    override fun initViews() {
        //关联底部导航组件
        navHostFragment = supportFragmentManager.findFragmentById(R.id.main_nav_host_fragment) as NavHostFragment
        navController = navHostFragment.navController
        mDataBinding.mainBottomNav.setupWithNavController(navController)

        //权限检查
        checkAppPermission()
    }

    private fun checkAppPermission() {
        //方案一：系统API
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
            requestPermissions(
                arrayOf(
                    Manifest.permission.INTERNET,
                    Manifest.permission.WRITE_EXTERNAL_STORAGE,
                    Manifest.permission.CAMERA,
                    Manifest.permission.RECORD_AUDIO,
                    Manifest.permission.READ_PHONE_STATE,
                    Manifest.permission.FOREGROUND_SERVICE
                ), 103
            )
        }
<<<<<<< HEAD
=======

>>>>>>> 4bd9ee2c4d9f0e81098ef6d1abae9f2eb7ec6c07

        //方案二：RxPermission
//        RxPermissions(this@MainActivity).request(
//            Manifest.permission.INTERNET,
//            Manifest.permission.WRITE_EXTERNAL_STORAGE,
//            Manifest.permission.CAMERA,
//            Manifest.permission.RECORD_AUDIO,
//            Manifest.permission.READ_PHONE_STATE,
//            Manifest.permission.FOREGROUND_SERVICE
//        ).subscribe()

<<<<<<< HEAD

=======
>>>>>>> 4bd9ee2c4d9f0e81098ef6d1abae9f2eb7ec6c07
    }


    override fun onRequestPermissionsResult(requestCode: Int, permissions: Array<out String>, grantResults: IntArray) {
        super.onRequestPermissionsResult(requestCode, permissions, grantResults)
        if (requestCode == 103){
            //判断返回权限
            if(!permissions.contains(Manifest.permission.INTERNET)){
                showToastMsg("无网络权限，请手动开启")
            }
            if(!permissions.contains(Manifest.permission.WRITE_EXTERNAL_STORAGE)){
                showToastMsg("无外部存储权限，请手动开启")
            }
            if(!permissions.contains(Manifest.permission.CAMERA)){
                showToastMsg("无相机权限，请手动开启")
            }
            if(!permissions.contains(Manifest.permission.RECORD_AUDIO)){
                showToastMsg("无麦克风权限，请手动开启")
            }
            if(!permissions.contains(Manifest.permission.READ_PHONE_STATE)){
                showToastMsg("无手机状态权限，请手动开启")
            }
            if(!permissions.contains(Manifest.permission.FOREGROUND_SERVICE)){
                showToastMsg("无前台服务权限，请手动开启")
            }
        }
    }

    /**
     * 打开应用详情页面
     */
    private fun openAppDetailPage(){
        var intent = Intent(Settings.ACTION_APPLICATION_DETAILS_SETTINGS)
        intent.data = Uri.fromParts("package", packageName, null)
        this@MainActivity.startActivity(intent)
    }


}